﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace NCRGEA.UserControls
{
    public partial class ucNotes : UserControl
    {
        private int _MemberID;
        public int MemberID
        {
            get
            {
                return _MemberID;
            }
            set
            {
                _MemberID = value;
                RefreshData();
            }
        }

        private bool _IsEditable;
        public bool IsEditable
        {
            get
            {
                return _IsEditable;
            }
            set
            {
                _IsEditable = value;
                UpdateEditable();
            }
        }

        public ucNotes()
        {
            InitializeComponent();
            IsEditable = false;
        }

        private void UpdateEditable()
        {
            txtNote.Enabled = _IsEditable;
            btnAddNote.Enabled = _IsEditable;
            lblNewNote.Enabled = _IsEditable;
        }

        private void btnAddNote_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtNote.Text))
                return;
            AddNote(txtNote.Text);
            RefreshData();
        }

        private void AddNote(string note)
        {
            string sqlQry = @"INSERT INTO logNotes (StaffID, MemberID, Date, Note) VALUES (@StaffID, @MemberID, @Date, @Note)";
            int StaffID = ((frmMain)ParentForm.MdiParent).CurrentUser.StaffID;
            SqlParameter[] sParamArray = new SqlParameter[4];
            sParamArray[0] = new SqlParameter("@StaffID", StaffID) { SqlDbType = SqlDbType.Int };
            sParamArray[1] = new SqlParameter("@MemberID", MemberID) { SqlDbType = SqlDbType.Int };
            sParamArray[2] = new SqlParameter("@Date", DateTime.Now) { SqlDbType = SqlDbType.DateTime };
            sParamArray[3] = new SqlParameter("@Note", txtNote.Text) { SqlDbType = SqlDbType.VarChar };
            int result = AAL.DAL.ExecuteNonQuery(sqlQry, sParamArray);
            if (result != 1)
                MessageBox.Show("An error occurred while adding the note!");
            txtNote.Text = string.Empty;
        }

        private void RefreshData()
        {
            string sqlQry = @"SELECT
	                            ua.Username,
                                ln.Date,
                                ln.Note
                            FROM
	                            logNotes ln
	                            LEFT JOIN
		                            usrAccounts ua
	                            ON
		                            ln.StaffID=ua.StaffID
                            WHERE
	                            ln.MemberID=@MemberID
                            ORDER BY
                                ln.Date DESC";
            SqlParameter[] sParamArray = new SqlParameter[1];
            sParamArray[0] = new SqlParameter("@MemberID", _MemberID) { SqlDbType = SqlDbType.Int };
            DataTable logs = AAL.DAL.ExecuteQuery(sqlQry, sParamArray);
            if (logs != null)
                grdNotes.DataSource = logs;
        }
    }
}
